// pages/bad_perf/bad_perf.js
import {
  slowJs,
  execLongTime
} from "../../utils/utils"
const app = getApp()

// 分包注入多耗点时
slowJs(100)

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // list: []
    showTime: Date.now(),
    a: false
  },

  onShow() {
    this.setData({
      showTime: Date.now()
    })
  },

  slowJs(e) {
    console.log("start test long time")
    console.log(this.slowJs.toString())
    wx.showLoading({
      title: '运行中',
    })
    execLongTime()
    slowJs(400)
    wx.hideLoading({
      success: (res) => {},
    })
  },

  slowRequest(e) {
    wx.request({
      url: 'https://stream.weixin.qq.com/weapp/SendMsg?sleep_time=5',
      method: 'GET',
      complete(data) {
        console.log('[req]: ', data)
      },
    })
  },

  badRequest(e) {
    wx.request({
      url: 'https://stream.weixin.qq.com/weapp/ReturnStatus?status=400',
      method: 'GET',
      complete(data) {
        console.log('[req1]: ', data)
      },
    })
    wx.request({
      url: 'https://stream.weixin.qq.com/weapp/ReturnStatus?status=401',
      method: 'GET',
      complete(data) {
        console.log('[req2]: ', data)
      },
    })
    wx.request({
      url: 'https://stream.weixin.qq.com/weapp/ReturnStatus?status=403',
      method: 'GET',
      complete(data) {
        console.log('[req3]: ', data)
      },
    })
    wx.request({
      url: 'https://www.baidu.com/',
      method: 'GET',
      complete(data) {
        console.log('[req4]: ', data)
      },
    })
  },

  tooMuchRequest(e) {
    const s = Date.now()
    for (let i = 0; i < 15; i++) {
      wx.request({
        url: `https://stream.weixin.qq.com/weapp/SendMsg?sleep_time=1&content=${s}${i}`,
        method: 'GET',
      })
    }
  },

  async tooMuchCloudRequest(e) {
    const now = Date.now()
    const ps = []
    for (let i = 0; i < 10; i++) {
      ps.push(wx.cloud.callFunction({
        name: 'quickstartFunctions',
        config: {
          env: app.globalData.selectedEnv.envId,
        },
        data: {
          type: 'delay',
          ms: 2000,
        },
      }).then(res => {res.cost = Date.now() - now;return res}).catch(err => {return {err, cost: Date.now() - now}}))
    }
    console.log("after", await Promise.all(ps))
  },

  repeatedRequest(e) {
    function doGet1(cb) {
      return wx.request({
        url: 'https://stream.weixin.qq.com/weapp/GetBigFile?file_size=131082',
        method: 'GET',
        success: cb || console
      })
    }
    const rTask = doGet1(data => {
      console.log('[get1]: ', data.data.length)
      return doGet1()
    })
    console.log(rTask)
  },

  setDataNoBind(e) {
    this.setData({
      "hehe": "123",
      "da": "adb",
      "ifcontent": "never show",
      "ifcontent2": "10",
    })
  },

  setDataTooFreq(e) {
    let current = 0
    let i = 0

    for (i = 0; i < 25; i++) {
      this.setData({
        [`list[${current + i}]`]: current + i
      })
    }

    current = current + i
  },

  setDataTooLarge(e) {
    let current = 0
    let i = 0
    let data = {}

    for (i = 0; i < 99999; i++) {
      data[`list[${current + i}]`] = current + i
    }

    current = current + i
    this.setData(data)
  },

  downloadFail(e) {
    wx.downloadFile({url: "https://stream.weixin.qq.com2/weapp/DownloadFile?file_name=test.png", complete:console.log}) // 域名问题
    wx.downloadFile({url: "https://stream.weixin.qq.com/weapp/DownloadFile?file_name=test3123123.png", complete:console.log})  // 500
  },

  downloadLongTime(e) {
    wx.showLoading({
      title: "下载中..."
    })
    wx.downloadFile({url: "https://stream.weixin.qq.com/weapp_dev/DownloadFile?file_name=a.txt&sleep_time=13", complete() {
      wx.hideLoading({
        success: (res) => {},
      })
    }})
  }
})